﻿using NewLife.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Models.ParameterModels;
using XingLucifer.Devices;
using XingLucifer.IBase.Enums;
using JsonReader = Newtonsoft.Json.JsonReader;
using JsonWriter = Newtonsoft.Json.JsonWriter;
using TypeConverter = System.ComponentModel.TypeConverter;

namespace UIWindows.Tools.Converter
{
    public class TupleJsonConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(Dictionary<(MachineInfoTypes, ShiftTypes), ObservableCollection<MesRunParameterModel>>);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jObject = JObject.Load(reader);
            var result = new Dictionary<(MachineInfoTypes, ShiftTypes), ObservableCollection<MesRunParameterModel>>();

            foreach (var property in jObject.Properties())
            {
                var parts = property.Name.Trim('(', ')').Split(',');
                var machineInfo = Enum.Parse<MachineInfoTypes>(parts[0].Trim());
                var shiftType = Enum.Parse<ShiftTypes>(parts[1].Trim());
                var key = (machineInfo, shiftType);

                var value = property.Value.ToObject<ObservableCollection<MesRunParameterModel>>(serializer);

                result.Add(key, value);
            }

            return result;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
    






    public class TupleConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var str = value as string;
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Trim('(', ')');
                var values = str.Split(',');
                var machineInfoType = Enum.Parse<MachineInfoTypes>(values[0].Trim());
                var shiftType = Enum.Parse<ShiftTypes>(values[1].Trim());
                return new Tuple<MachineInfoTypes, ShiftTypes>(machineInfoType, shiftType);
            }
            return base.ConvertFrom(context, culture, value);
        }
    }

    //[TypeConverter(typeof(TupleConverter))]
    public struct MyTuple
    {
        public MachineInfoTypes Item1 { get; set; }
        public ShiftTypes Item2 { get; set; }
    }
    public class MachineShiftKeyConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string s)
            {
                // 在这里添加你的转换逻辑
                // 例如：return MachineShiftKey.Parse(s);
                // 你需要根据你的实际需求来修改这个方法
                throw new NotImplementedException();
            }

            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string) && value is MachineShiftKey key)
            {
                // 在这里添加你的转换逻辑
                // 例如：return key.ToString();
                // 你需要根据你的实际需求来修改这个方法
                throw new NotImplementedException();
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
    [TypeConverter(typeof(MachineShiftKeyConverter))]
    public class MachineShiftKey
    {
        public MachineInfoTypes Machine { get; set; }
        public ShiftTypes Shift { get; set; }
    }
}
